package com.fieldchain.ex.fa100.ui.personal.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.BankBean;
import com.fieldchain.ex.fa100.bean.User;
import com.fieldchain.ex.fa100.inter.IntegerCallBack;
import java.util.ArrayList;
import java.util.List;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.ListRequest;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.fieldchain.ex.fa100.utils.share.ShareManager;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/12
 */
public class BankActivity extends MyBaseActivity {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private RecyclerView rv;
    private RelativeLayout reaEmpty;
    private TextView tvEmpty;
    private ImageView ivAdd;

    private BankAdapter adapter;
    int type=0;
    private List<BankBean> mList=new ArrayList<>();

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        rv = findViewById(R.id.rv);
        reaEmpty = findViewById(R.id.rea_empty);
        tvEmpty = findViewById(R.id.tv_empty);
        ivAdd = findViewById(R.id.iv_add);
    }

    @Override
    public void setValue() {
        type=getIntent().getIntExtra("type",0);
        tvCenter.setText("添加银行卡");
        adapter = new BankAdapter(mContext);
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setAdapter(adapter);

        adapter.setIntegerCallBack(new IntegerCallBack() {
            @Override
            public void index(int pos) {
                if(type==5){
                    Intent intent=new Intent();
                    intent.putExtra("id",mList.get(pos).getId());
                    setResult(5,intent);
                    finish();
                }
            }
        });
        reaBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User user=ShareManager.getInstance().getUser();
                if(user.getTpwFlag()==1) {
                    startActivity(new Intent(mContext, AddBankActivity.class));
                }else{
                    MyToast.show("未设置交易密码");
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_add;
    }

    public void getData() {
        HttpRequest.getInstance().getRequest(Constant.bankList,new ListRequest<BankBean>() {
            @Override
            public void onSucess(List<BankBean> list, int code, String msg) {
                if (code == 200) {
                    adapter.setData(list);

                    mList.clear();
                    mList.addAll(list);
                }
                if (list == null || list.size() == 0) {
                    reaEmpty.setVisibility(View.VISIBLE);
                } else {
                    reaEmpty.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFaild(Exception e) {
                reaEmpty.setVisibility(View.VISIBLE);
            }
        });
    }


}
